import 'package:flutter/material.dart';
import 'package:simple_animations/simple_animations.dart';
import 'package:supercharged/supercharged.dart';

import '../../common/common.dart';


enum MyInkWellAnimation {
  none,
  zoomOut, //缩小
  zoomIn, //放大
  ink, //水波纹
}

class MyInkWell extends StatefulWidget {
  final Widget child;
  final VoidCallback? onTap;
  final VoidCallback? onLongPress;

  ///缩放 默认值 0.9
  final double scaleDown;
  final MyInkWellAnimation animation;
  final Color splashColor;



  const MyInkWell({
    Key? key,
    required this.child,
    this.onTap,
    this.onLongPress,
    this.scaleDown = 0.9,
    this.animation = MyInkWellAnimation.zoomOut,
    this.splashColor = Colors.black12,
  }) : super(key: key);

  @override
  _MyInkWellState createState() => _MyInkWellState();
}

class _MyInkWellState extends State<MyInkWell> {
  bool isDown = false;

  void onTap() {
    if (widget.onTap != null) {
      myException(() {
        widget.onTap!();
      });
    }
  }

  void onLongPress() {
    if (widget.onLongPress != null) {
      myException(() {
        widget.onLongPress!();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _widget(),
    );
  }

  Widget _widget() {
    switch (widget.animation) {
      case MyInkWellAnimation.none:
        return InkWell(
          child: widget.child,
          onTap: () {
            onTap();
          },
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent,
        );
      case MyInkWellAnimation.ink:
        return Material(
          color: Colors.transparent,
          child: InkWell(
            child: widget.child,
            onTap: () {
              onTap();
            },
            splashColor: widget.splashColor,
            highlightColor: Colors.transparent,
          ),
        );

      case MyInkWellAnimation.zoomIn:
        return CustomAnimationBuilder<double>(
          duration: const Duration(milliseconds: 200),
          tween: Tween<double>(begin: 1.0, end: widget.scaleDown),
          //tween: (1.0).tweenTo(widget.scaleDown),
          control: isDown ? Control.play : Control.playReverse,
          builder: (context, value, child) {
            return Transform.scale(
              scale: value,
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  child: widget.child,
                  onTap: () {
                    delay(100, () {
                      setState(() {
                        isDown = false;
                      });
                    });

                    delay(180, () {
                      onTap();
                    });
                  },
                  onTapCancel: () {
                    delay(50, () {
                      setState(() {
                        isDown = false;
                      });
                    });
                  },
                  onTapDown: (_) {
                    if(widget.onTap == null && widget.onLongPress == null){
                      return;
                    }
                    setState(() {
                      isDown = true;
                    });
                  },
                  splashColor: Colors.transparent,
                  highlightColor: Colors.transparent,
                  //enableFeedback: ,
                ),
              ),
            );
          },
        );
      case MyInkWellAnimation.zoomOut:
        return CustomAnimationBuilder<double>(
          duration: const Duration(milliseconds: 200),
          tween: (1.0).tweenTo(widget.scaleDown),
          control: isDown ? Control.play : Control.playReverse,
          builder: (context, value, child) {
            return Transform.scale(
              scale: value,
              child: Material(
                color: Colors.transparent,
                child: InkWell(
                  child: widget.child,
                  onTap: () {
                    delay(100, () {
                      setState(() {
                        isDown = false;
                      });
                    });

                    delay(180, () {
                      onTap();
                    });
                  },
                  onTapCancel: () {
                    delay(50, () {
                      setState(() {
                        isDown = false;
                      });
                    });
                  },
                  onTapDown: (_) {
                    if(widget.onTap == null && widget.onLongPress == null){
                      return;
                    }
                    setState(() {
                      isDown = true;
                    });
                  },
                  splashColor: Colors.transparent,
                  highlightColor: Colors.transparent,
                  //enableFeedback: ,
                ),
              ),
            );
          },
        );
    }
  }
}
